﻿#include "DownloadAutomaticCmd.h"

DownloadAutomaticCmd::DownloadAutomaticCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    m_automaticIndex = 0;
}

Package DownloadAutomaticCmd::toPackage()
{
    Package package;
    return package;
}

void DownloadAutomaticCmd::unPackage(Package package)
{
    m_cmdId = package.id();
    QByteArray body;

  //  body = decrypt(package.body(),KeyType::AES);
    QString bodydata=body.toHex();
    qDebug()<<"DownloadAutomaticCmd-Decrypt body="<<bodydata;

    quint16 index = 0;
    quint16 strLength;
    m_automaticIndex = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    quint16 automaticSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    quint64 automaticId = body.mid(index,8).toHex().toInt(0,16);
    index += 8;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    qDebug()<<"strlengthbody1="<<QString::number(body.mid(index,2).toInt());
    qDebug()<<"strlength="<<QString::number(strLength);
    index += 2;
    QString automaticName = QString::fromUtf8(body.mid(index,strLength));
    index += strLength;

    quint16 automaticIcoId = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    quint8 isExecute = body.mid(index,1).toHex().toInt(0,16);
    index += 1;

    quint8 conditionSatisfyType = body.mid(index,1).toHex().toInt(0,16);
    index += 1;

    quint16 actionSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;

    QList<ActionStruct> actionList;
    for(int i=0; i<actionSize; i++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString zoneName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString deviceName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString functionName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString valueName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        quint16 zoneCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceTypeCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionValue = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 address = body.mid(index,2).toHex().toInt(0,16);
        index += 2;


        quint16 delayTime = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        ActionStruct action;
        action.setZoneName(zoneName);
        action.setDeviceName(deviceName);
        action.setFunctionName(functionName);
        action.setValueName(valueName);
        action.setZoneCode(zoneCode);
        action.setDeviceTypeCode(deviceTypeCode);
        action.setDeviceCode(deviceCode);
        action.setFunctionCode(functionCode);
        action.setFunctionValue(functionValue);
        action.setAddress(address);
        action.setDelayTime(delayTime);
        actionList.append(action);
    }
    quint16 equipmentConditionSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    printf("DownloadAutomaticCmd::unPackage equipmentConditionSize- %d\r\n",equipmentConditionSize);

    QList<EquipmentConditionStruct> equipmentConditionList;
    for(int i=0; i<equipmentConditionSize; i++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString zoneName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString deviceName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString functionName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QString valueName = QString::fromUtf8(body.mid(index,strLength));
        index += strLength;

        quint16 zoneCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceTypeCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 deviceCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionCode = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 functionValue = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 address = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 number = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint8 conditionType = body.mid(index,1).toHex().toInt(0,16);
        index += 1;

        EquipmentConditionStruct equipmentCondition;
        equipmentCondition.setZoneName(zoneName);
        equipmentCondition.setDeviceName(deviceName);
        equipmentCondition.setFunctionName(functionName);
        equipmentCondition.setValueName(valueName);
        equipmentCondition.setZoneCode(zoneCode);
        equipmentCondition.setDeviceTypeCode(deviceTypeCode);
        equipmentCondition.setDeviceCode(deviceCode);
        equipmentCondition.setFunctionCode(functionCode);
        equipmentCondition.setFunctionValue(functionValue);
        equipmentCondition.setAddress(address);
        equipmentCondition.setNumber(number);
        equipmentCondition.setConditionType(conditionType);
        equipmentConditionList.append(equipmentCondition);
    }


    quint16 timeConditionSize = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
   printf("DownloadAutomaticCmd::unPackage timeConditionSize- %d\r\n",timeConditionSize);
    QList<TimeConditionStruct> timeConditionList;

    for(int i=0; i<timeConditionSize; i++)
    {
        strLength = body.mid(index,2).toHex().toInt(0,16);
        index += 2;
        QByteArray timeBCD = body.mid(index,strLength);
        index += strLength;

        quint8 repeat = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        quint16 number = body.mid(index,2).toHex().toInt(0,16);
        index += 2;

        QString tempTime;
        for(int j=0; j<timeBCD.size(); j++)
        {
            quint8 temp = (timeBCD.at(j)/16*10)+(timeBCD.at(j)&0x0F);
            tempTime.append(QString::number(temp).sprintf("%02d",temp));
        }
        QString date = tempTime.mid(0,8);
        date.insert(4,"-");
        date.insert(7,"-");

        QString time = tempTime.mid(8,8);
        time.insert(2,":");
        time.insert(5,":");
        QString completeTime;
        if(repeat == 0)
        {
            completeTime = date + " " + time;
            TimeConditionStruct timeCondition;
            timeCondition.setTime(completeTime);
            timeCondition.setNumber(number);
            timeConditionList.append(timeCondition);
        }
        else
        {
            for(int j=0; j<7; j++)
            {
                if(repeat%2 == 1)
                {
                    switch(j)
                    {
                        case 0: completeTime = "Monday "    + time; break;
                        case 1: completeTime = "Tuesday "   + time; break;
                        case 2: completeTime = "Wednesday " + time; break;
                        case 3: completeTime = "Thursday "  + time; break;
                        case 4: completeTime = "Friday "    + time; break;
                        case 5: completeTime = "Saturday "  + time; break;
                        case 6: completeTime = "Sunday "    + time; break;
                        default: completeTime.clear(); break;
                    }
                    if(!completeTime.isEmpty())
                    {
                        TimeConditionStruct timeCondition;
                        timeCondition.setTime(completeTime);
                        timeCondition.setNumber(number);
                        timeConditionList.append(timeCondition);
                    }
                }
                repeat = repeat>>1;
            }
        }
    }

    Q_UNUSED(automaticSize);



    m_automatic.setIsExecute(isExecute);
    m_automatic.setId(automaticId);
    m_automatic.setName(automaticName);
    m_automatic.setIcoId(automaticIcoId);
    m_automatic.setConditionSatisfyType(conditionSatisfyType);
    m_automatic.setActionList(actionList);

    m_automatic.setEquipmentConditionList(equipmentConditionList);
    m_automatic.setTimeConditionList(timeConditionList);
     //printf("DownloadAutomaticCmd::unPackage 11timeConditionSize- %d\r\n",timeConditionList.size());
}
quint16 DownloadAutomaticCmd::automaticIndex() const
{
    return m_automaticIndex;
}

void DownloadAutomaticCmd::setAutomaticIndex(const quint16 &automaticIndex)
{
    m_automaticIndex = automaticIndex;
}
AutomaticStruct DownloadAutomaticCmd::automatic() const
{
    return m_automatic;
}

void DownloadAutomaticCmd::setAutomatic(const AutomaticStruct &automatic)
{
    m_automatic = automatic;
}


